package top.hcode.hoj.service.oj.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.mapper.ClazzMapper;
import top.hcode.hoj.mapper.MajorMapper;
import top.hcode.hoj.mapper.UserInfoMapper;
import top.hcode.hoj.pojo.entity.user.Clazz;
import top.hcode.hoj.pojo.entity.user.Major;
import top.hcode.hoj.pojo.entity.user.UserInfo;
import top.hcode.hoj.service.oj.ClazzService;

import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
        private ClazzMapper clazzMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;


    @Autowired
    private MajorMapper majorMapper;


    @Override
    public CommonResult<List<Clazz>> getdeplists(String selectedId) {
        return   CommonResult.successResponse(clazzMapper.getdeplists(selectedId));
    }

    @Override
    public CommonResult<List<Clazz>> getLits() {
        List<Clazz> clazzes  = clazzMapper.selectList(null);
        for (Clazz clazz : clazzes) {
            if (clazz.getMajorid()!=null) {
                Major major =  majorMapper.selectById(clazz.getMajorid());
                clazz.setMajorid(major.getName());
            }
        }
        return CommonResult.successResponse(clazzes);
    }

    @Override
    public CommonResult<Void> addclazz(Clazz clazz) {
        String uuid20 = String.format("%d%019d", new java.util.Random().nextInt(9) + 1, new java.util.Random().nextLong()).substring(0, 20);
        clazz.setId(uuid20);
List<Clazz> majors = clazzMapper.selectList(null);
        boolean b = majors.stream().anyMatch(m ->
                m.getMajorid().equals(clazz.getMajorid()) &&
                        m.getName().equals(clazz.getName())
        );
        if (b) {
            return  CommonResult.errorResponse("同一专业下班级不可重复");
        }
        int result =clazzMapper.insert(clazz);
        if (result > 0) {
            return CommonResult.successResponse("添加成功");
        } else {
            return CommonResult.errorResponse("添加失败");
        }
    }

    @Override
    public CommonResult<Void> updateclazz(Clazz clazz) {
        if (clazz.getMajorid() == null|| clazz.getMajorid().equals("")) {
            return  CommonResult.errorResponse("专业不可以为空");
        }
        if(clazz.getName() == null || clazz.getName().equals("")){
            return  CommonResult.errorResponse("班级名称不可以为空");
        }
        List<Clazz> majors = clazzMapper.selectList(null);
        boolean b = majors.stream().anyMatch(m ->
                m.getMajorid().equals(clazz.getMajorid()) &&
                        m.getName().equals(clazz.getName())
        );
        if (b) {
            return  CommonResult.errorResponse("同一专业下班级不可重复");
        }
        int result =clazzMapper.updateById(clazz);
        if (result > 0) {
            return CommonResult.successResponse("添加成功");
        } else {
            return CommonResult.errorResponse("添加失败");
        }
    }

    @Override
    public CommonResult<Void> deleteclazz(Clazz clazz) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("clazzid", clazz.getId()); // WHERE majorid = #{majorid}
        List<UserInfo> userList = userInfoMapper.selectList(wrapper);
        if (userList.size() > 0) {
            return CommonResult.errorResponse("已经绑定学生不可删除");
        }else {
            clazzMapper.deleteById(clazz.getId());
            return CommonResult.successResponse();
        }
    }

}
